package LeetCode.leetcode107;


import java.util.*;

public class Solution {
    /**
     *
     * @param root
     * @return
     */
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
//        Stack<List<TreeNode>> stack = new Stack<>();
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode tmp;
        if (root == null)
            return res;
        queue.offer(root);
        while (!queue.isEmpty()){
            List<Integer> lines = new ArrayList<>();
            int length = queue.size();
            while (length > 0){
                tmp = queue.poll();
                lines.add(tmp.val);
                if (tmp.left != null)
                    queue.offer(tmp.left);
                if (tmp.right != null)
                    queue.offer(tmp.right);
                length--;
            }
            res.add(0,lines);
        }
        return res;
    }
}

  class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

